<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/1/12
 * Time: 15:01
 */

namespace app\api\model;


use think\Db;

class Label extends BaseModel
{
    protected $autoWriteTimestamp = true;

    /**
     * 与user表多对多关系
     * @return \think\model\relation\BelongsToMany
     */
    public function users(){
        return $this->belongsToMany('User','user_label','user_id','label_id');
    }
    /**
     * 请求标签列表
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getLabelList($wu_id){
        $list = self::where('wu_id','=',$wu_id)
            ->with(['users'])
            ->select();
        return $list;
    }

    /**
     * 请求标签用户
     * @param $id
     * @return \think\model\relation\BelongsToMany
     */
    public static function getUuserByLabel($id){
           $label =  self::where('id','=',$id)
               ->with(['users'])->find();
           $label->users->visible(['id','avatar_url','nickname']);
            return $label;
    }

    /**
     * 删除标签
     * @param $id
     * @return int
     */
    public static function deleteLabel($id){
        // 启动事务
        Db::startTrans();
        try{
            self::destroy($id);
            $status = UserLabel::Where('label_id','=',$id)->delete();
            // 提交事务
            Db::commit();
            return $status;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return "";
        }

    }
    /**
     * 更新标签
     * @param $data
     * @param $users
     * @param $id
     * @return $this
     */
    public static function updateLabel($data,$users,$id){
        Db::startTrans();
        try{
             self::where('id','=',$id)->update($data);//更新用户信息
            UserLabel::Where('label_id','=',$id)->delete();//删除原有关系
            //更新关系
            if ($users != null){
                foreach ($users as $key=>$value){
                    $info[$key]['user_id'] = $value;
                    $info[$key]['label_id'] = $id;
                }
                $status  = (new UserLabel())->saveAll($info);//新建关系
            }
            // 提交事务
            Db::commit();
            return "success";
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return "";
        }
    }
    /**
     * 添加用户标签
     * @param $data
     * @param $users
     * @return $this
     */
    public static function addLabel($data,$wu_id,$users){
        Db::startTrans();
        try{
            $label = self::create($data);
            if ($users != null){
                foreach ($users as $key=>$value){
                    $info[$key]['user_id'] = $value;
                    $info[$key]['label_id'] = $label->id;
                    $info[$key]['wu_id'] = $wu_id;
                }
                (new UserLabel())->saveAll($info);//新建关系
            }
            Db::commit();
            return $label;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return "";
        }
    }

    /**
     * 查询标签
     * @param $wu_id
     * @param $name
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getLabel($wu_id,$name){
        return self::where('wu_id','=',$wu_id)
            ->where('name','=',$name)
            ->find();
    }


}